const fs = require('fs');
const citys = require('./citys.json');

const columnRules = [
    {
        column: '基础信息',
        children: [
            {column: '排名', children: []},
            {column: '面积(平方千米)'},
            {column: '等级'}
        ],
    },
    {
        column: '基础信息',
        children: [
            {column: '排名'},
            {column: '面积(平方千米)'},
            {column: '等级'}
        ],
    },
    {
        column: '基础信息',
        children: [
            {column: '排名'},
            {column: '面积(平方千米)'},
            {column: '等级'}
        ],
    }
];

const parse = async () => {
    const citysPivot = [];

    const indicatorMap = {
        ranking: '排名',
        level: '等级',
        area: '面积(平方千米)',
        peopleCount: '人口数量',
        avgAge: '平均年龄',
        sexualRate: '男女比例',
        gdp: 'GDP(亿)',
        industryType: '行业类型',
        avgSalary: '平均工资',
        empRate: '就业率(%)',
    };
    citys.forEach(item => {
        const { cityName, region } = item;
        
        const keys = Object.keys(item);
        keys.forEach(key => {
            if (['ranking', 'level', 'area'].includes(key)) {
                citysPivot.push({
                    cityName,
                    region,
                    type: '基础信息',
                    indicator: indicatorMap[key],
                    value: item[key],
                });
            } else if (['peopleCount', 'avgAge', 'sexualRate'].includes(key)) {
                citysPivot.push({
                    cityName,
                    region,
                    type: '人口信息',
                    indicator: indicatorMap[key],
                    value: item[key],
                });
            } else if (['gdp', 'industryType', 'avgSalary', 'empRate'].includes(key)) {
                citysPivot.push({
                    cityName,
                    region,
                    type: '经济数据',  // 修改为 economicInfo 而不是 economyInfo
                    indicator: indicatorMap[key],
                    value: key == 'empRate' ? item[key] * 100 : item[key],
                });
            }
        });
    });

    try {
        const filename = './citysPivot.json';
        // 将对象转换为格式化的 JSON 字符串（带缩进）
        const jsonString = JSON.stringify(citysPivot, null, 2);
        // 写入文件
        fs.writeFileSync(filename, jsonString, 'utf8');
        console.log(`已成功写入 ${filename}`);
    } catch (err) {
        console.error('写入文件时出错:', err);
    }
}

parse();